<?php
namespace Home\Controller;
use Think\Controller;

class TimeController extends Controller{
    public function setTime(){
        $userID=isset($_POST['userId'])?trim($_POST['userId']):null;       //用户ID
        $time=isset($_POST['time'])?trim($_POST['time']):null;       //时间
        if(empty($userID)||empty($time)){
            $requset=array(
                'returnCode' => 998,
                'returnMsg' => '参数错误'
            );
            echo json_encode($requset); exit;
        }else{
            $data['testTime']=strtotime($time);
            $re=M('user')->where(['id'=>$userID])->save($data);
            if($re){
              $requset=array('returnCode'=>0,'returnMsg'=>'成功');
            }else{
              $requset=array('returnCode'=>999,'returnMsg'=>'失败');
            }
        }
            echo json_encode($requset); exit;
    }
   public function getTime(){
       $userID=isset($_POST['userId'])?trim($_POST['userId']):null;       //用户ID
       if(empty($userID)){
           $requset=array(
               'returnCode' => 998,
               'returnMsg' => '参数错误'
           );
           echo json_encode($requset); exit;
       }else{
           $re=M('user')->where(['id'=>$userID])->field('testTime')->find();
           if(!empty($re)){
               $startdate=time();
               $enddate=$re['testTime'];
//                $days['days']=round(($enddate-$startdate)/86400);
               $days['days']=round(($enddate-$startdate)/86400)+1;
               $requset=array('returnCode'=>0,'returnMsg'=>'返回成功','returnData'=>$days);
           }else{
               $requset=array('returnCode'=>999,'returnMsg'=>'空数据','returnData'=>null);
           }
       }
               echo json_encode($requset); exit;
   }
}